home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / misc / AmigaSDLsrc.lha / amisrc / SDL_memops.h < prev    next >
C/C++ Source or Header  |  2001-04-29  |  3KB  |  128 lines

  1. /*
  2.     SDL - Simple DirectMedia Layer
  3.     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public
  16.     License along with this library; if not, write to the Free
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     Sam Lantinga
  20.     slouken@devolution.com
  21. */
  22.  
  23. #ifdef SAVE_RCSID
  24. static char rcsid =
  25.  "@(#) $Id: SDL_memops.h,v 1.1.2.16 2001/02/22 01:14:05 hercules Exp $";
  26. #endif
  27.  
  28. #ifndef _SDL_memops_h
  29. #define _SDL_memops_h
  30.  
  31. /* System dependent optimized memory manipulation routines:
  32. */
  33. #include <string.h>
  34.  
  35. #if defined(__GNUC__) && defined(i386)
  36. /* Thanks to Brennan "Bas" Underwood, for the inspiration. :)
  37.  */
  38. #define SDL_memcpy(dst, src, len)                      \
  39. do {                                      \
  40.     int u0, u1, u2;                          \
  41.     __asm__ __volatile__ (                          \
  42.         "cld\n\t"                          \
  43.         "rep ; movsl\n\t"                      \
  44.         "testb $2,%b4\n\t"                      \
  45.         "je 1f\n\t"                          \
  46.         "movsw\n"                          \
  47.         "1:\ttestb $1,%b4\n\t"                      \
  48.         "je 2f\n\t"                          \
  49.         "movsb\n"                          \
  50.         "2:"                              \
  51.         : "=&c" (u0), "=&D" (u1), "=&S" (u2)              \
  52.         : "0" ((unsigned)(len)/4), "q" (len), "1" (dst),"2" (src) \
  53.         : "memory" );                          \
  54. } while(0)
  55.  
  56. #define SDL_revcpy(dst, src, len)            \
  57. do {                            \
  58.     int u0, u1, u2;                    \
  59.     char *dstp = (char *)(dst);            \
  60.     char *srcp = (char *)(src);            \
  61.     int n = (len);                    \
  62.     if ( n >= 4 ) {                    \
  63.     __asm__ __volatile__ (                \
  64.         "std\n\t"                \
  65.         "rep ; movsl\n\t"            \
  66.         : "=&c" (u0), "=&D" (u1), "=&S" (u2)    \
  67.         : "0" (n >> 2),                \
  68.           "1" (dstp+(n-4)), "2" (srcp+(n-4))    \
  69.         : "memory" );                \
  70.     }                        \
  71.     switch (n & 3) {                \
  72.         case 3: dstp[2] = srcp[2];        \
  73.         case 2: dstp[1] = srcp[1];        \
  74.         case 1: dstp[0] = srcp[0];        \
  75.             break;                \
  76.         default:                \
  77.             break;                \
  78.     }                        \
  79. } while(0)
  80.  
  81. #define SDL_memmove(dst, src, len)            \
  82. do {                            \
  83.     if ( (dst) < (src) ) {                \
  84.         SDL_memcpy((dst), (src), (len));    \
  85.     } else {                    \
  86.         SDL_revcpy((dst), (src), (len));    \
  87.     }                        \
  88. } while(0)
  89.  
  90. #define SDL_memset4(dst, val, len)                \
  91. do {                                \
  92.     int u0, u1, u2;                    \
  93.     __asm__ __volatile__ (                    \
  94.         "cld\n\t"                    \
  95.         "rep ; stosl\n\t"                \
  96.         : "=&D" (u0), "=&a" (u1), "=&c" (u2)        \
  97.         : "0" (dst), "1" (val), "2" ((Uint32)(len))    \
  98.         : "memory" );                    \
  99. } while(0)
  100.  
  101. #endif /* GNU C and x86 */
  102.  
  103. /* If there are no optimized versions, define the normal versions */
  104. #ifndef SDL_memcpy
  105. #define SDL_memcpy(dst, src, len)    memcpy(dst, src, len)
  106. #endif
  107. #ifndef SDL_revcpy
  108. #define SDL_revcpy(dst, src, len)    memmove(dst, src, len)
  109. #endif
  110. #ifndef SDL_memset4
  111. #define SDL_memset4(dst, val, len)        \
  112. do {                        \
  113.     unsigned _count = (len);        \
  114.     unsigned _n = (_count + 3) / 4;        \
  115.     Uint32 *_p = (Uint32 *)(dst);        \
  116.     Uint32 _val = (val);            \
  117.         switch (_count % 4) {            \
  118.         case 0: do {    *_p++ = _val;        \
  119.         case 3:         *_p++ = _val;        \
  120.         case 2:         *_p++ = _val;        \
  121.         case 1:         *_p++ = _val;        \
  122.         } while ( --_n );        \
  123.     }                    \
  124. } while(0)
  125. #endif
  126.  
  127. #endif /* _SDL_memops_h */
  128.